package com.view
{
	import com.util.DragTracer;
	
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	import fr.seraf.wow.constraint.WRigidConstraint;
	import fr.seraf.wow.constraint.WSpringConstraint;
	import fr.seraf.wow.core.WOWEngine;
	import fr.seraf.wow.core.data.WPlane;
	import fr.seraf.wow.core.data.WVector;
	import fr.seraf.wow.primitive.WOWPlane;
	import fr.seraf.wow.primitive.WSphere;
	
	import org.papervision3d.core.geom.Lines3D;
	import org.papervision3d.core.geom.renderables.Line3D;
	import org.papervision3d.core.geom.renderables.Vertex3D;
	import org.papervision3d.core.math.Number3D;
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.materials.WireframeMaterial;
	import org.papervision3d.materials.special.LineMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.view.BasicView;
	
	public class SceneConstraint extends BasicView
	{
		//Variables
		private var _sphere1:Sphere;
		private var _sphere2:Sphere;
		private var _sphere3:Sphere;
		private var _wowSphere1:WSphere;
		private var _wowSphere2:WSphere;
		private var _wowSphere3:WSphere;
		private var _wow:WOWEngine;
		private var _wowConstraint1:WSpringConstraint;
		private var _wowConstraint2:WRigidConstraint;
		private var _lines:Lines3D;
		
		//Constructor
		public function SceneConstraint()
		{
			super(1024, 800, true, true );
			this.initPhysics();
			this.init3D();
		}
		
		//Public methods
		
		//Private methods
		private function initPhysics():void
		{
			this._wow = new WOWEngine();
			this._wow.collisionResponseMode = this._wow.SELECTIVE;
			
			this._wow.addMasslessForce( new WVector( 0, 15, 0 ) );
		}
		
		private function init3D():void
		{
			//3D
			var mWire:WireframeMaterial = new WireframeMaterial( 0x00FF00, 1 );
			this._sphere1 = new Sphere( mWire, 100, 10, 10 );
			this._sphere1.material.doubleSided = true;
			this._sphere1.material.interactive = true;
			this._sphere1.useOwnContainer = true;
			
			this._sphere1.y = 400;
			
			this._sphere2 = new Sphere( mWire, 100, 10, 10 );
			this._sphere2.material.doubleSided = true;
			
			this._sphere2.x = 500;
			this._sphere2.y = 200;
			
			this._sphere3 = new Sphere( mWire, 100, 10, 10 );
			this._sphere3.material.doubleSided = true;
			
			this._sphere3.x = -500;
			this._sphere3.y = 200;
			
			this._lines = new Lines3D( new LineMaterial() );
			
			scene.addChild( this._lines );
			scene.addChild( this._sphere1 );
			scene.addChild( this._sphere2 );
			scene.addChild( this._sphere3 );
			
			
			this._lines.addLine( new Line3D( this._lines, new LineMaterial(), 1, new Vertex3D( this._sphere1.x, this._sphere1.y, this._sphere1.z ), new Vertex3D( this._sphere2.x, this._sphere2.y, this._sphere2.z ) ) );
			this._lines.addLine( new Line3D( this._lines, new LineMaterial(), 1, new Vertex3D( this._sphere1.x, this._sphere1.y, this._sphere1.z ), new Vertex3D( this._sphere3.x, this._sphere3.y, this._sphere3.z ) ) );
			
			//Physics
			this._wowSphere1 = new WSphere( this._sphere1.x, -this._sphere1.y, this._sphere1.z, 100, true , .1, .5, .02 );
			this._wow.addParticle( this._wowSphere1 );
			
			this._wowSphere2 = new WSphere( this._sphere2.x, -this._sphere2.y, this._sphere2.z, 100, false ,1, .2, .3 );
			this._wow.addParticle( this._wowSphere2 );
			
			this._wowSphere3 = new WSphere( this._sphere3.x, -this._sphere3.y, this._sphere3.z, 100, false ,.2, .2, .3 );
			this._wow.addParticle( this._wowSphere3 );
			
			this._wowConstraint1 = new WSpringConstraint( this._wowSphere1, this._wowSphere2, .7 );
			this._wow.addConstraint( this._wowConstraint1 );
			
			this._wowConstraint2 = new WRigidConstraint( this._wowSphere1, this._wowSphere3 ); //new WSpringConstraint( this._wowSphere1, this._wowSphere3, .7 );
			this._wow.addConstraint( this._wowConstraint2 );
			
			this.addEventListener( Event.ENTER_FRAME, updateScene );
		}
		
		//Event handlres
		
		private function updateScene( $event:Event ):void
		{
			this._wow.step();
			this.singleRender();
			
			
			this._sphere2.x = this._wowSphere2.px;
			this._sphere2.y = -this._wowSphere2.py;
			this._sphere2.z = this._wowSphere2.pz;
			
			this._sphere3.x = this._wowSphere3.px;
			this._sphere3.y = -this._wowSphere3.py;
			this._sphere3.z = this._wowSphere3.pz;
			
			this._lines.removeAllLines();
			this._lines.addLine( new Line3D( this._lines, new LineMaterial(), 1, new Vertex3D( this._sphere1.x, this._sphere1.y, this._sphere1.z ), new Vertex3D( this._sphere2.x, this._sphere2.y, this._sphere2.z ) ) );
			this._lines.addLine( new Line3D( this._lines, new LineMaterial(), 1, new Vertex3D( this._sphere1.x, this._sphere1.y, this._sphere1.z ), new Vertex3D( this._sphere3.x, this._sphere3.y, this._sphere3.z ) ) );
			
			camera.x = ( viewport.containerSprite.mouseX * 1.5 );
			camera.y = ( viewport.containerSprite.mouseY * 1.5 );
		}
	}
}